/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package firefighter;

import javax.microedition.lcdui.*;

/**
 * @author matt
 */
public class Splash extends Canvas implements Runnable {
    private final Firefighter midlet;
    private Image splashImage;
    private volatile boolean dismissed = false;

    Splash(Firefighter midlet)
    {
        this.midlet = midlet;
        setFullScreenMode(true);
        splashImage = Firefighter.createImage("/splash.png");
        new Thread(this).start();
    }

    public void run()
    {
        synchronized(this)
        {
            try
            {
                this.wait(3000L);
                // this.wait(3000);     forces a 3second delay
            }
            catch (InterruptedException e)
            {
                // do nothing
            }
            dismiss();
        }
    }

    public void paint(Graphics g)
    {
        int width = getWidth();
        int height = getHeight();
        g.setColor(0x00FFFFFF); // white
        g.fillRect(0, 0, width, height);
        g.setColor(0x00FF0000); // red
        g.drawRect(1, 1, width-3, height-3); // red border one pixel from edge
        if (splashImage != null)
        {
            g.drawImage(splashImage, width/2, height/2, Graphics.VCENTER | Graphics.HCENTER);
            splashImage = null;
            midlet.splashScreenPainted();
        }
    }

    public synchronized void keyPressed(int keyCode)
    {
        dismiss();
    }

    private void dismiss()
    {
        if (!dismissed)
        {
            dismissed = true;
            midlet.splashScreenDone();
        }
    }
}